package com.example.accountingapp;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class AccountSecurityActivity extends AppCompatActivity {

    private DatabaseHelper databaseHelper;
    private EditText etUsername, etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_security);

        databaseHelper = new DatabaseHelper(this);
        etUsername = findViewById(R.id.etUsername);
        etPassword = findViewById(R.id.etPassword);

        Button btnRegister = findViewById(R.id.btnRegister);
        Button btnModifyPassword = findViewById(R.id.btnModifyPassword);
        Button btnDeleteAccount = findViewById(R.id.btnDeleteAccount);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUsername.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                if (!username.isEmpty() && !password.isEmpty()) {
                    boolean result = databaseHelper.addUser(username, password);
                    if (result) {
                        Toast.makeText(AccountSecurityActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(AccountSecurityActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(AccountSecurityActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btnModifyPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUsername.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                if (!username.isEmpty() && !password.isEmpty()) {
                    boolean result = databaseHelper.updateUser(username, password);
                    if (result) {
                        Toast.makeText(AccountSecurityActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(AccountSecurityActivity.this, "密码修改失败", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(AccountSecurityActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btnDeleteAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUsername.getText().toString().trim();
                if (!username.isEmpty()) {
                    boolean result = databaseHelper.deleteUser(username);
                    if (result) {
                        Toast.makeText(AccountSecurityActivity.this, "账号删除成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(AccountSecurityActivity.this, "账号删除失败", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(AccountSecurityActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
